{% extends 'base.html' %}
{% load static %}

{% block title %}资产列表{% endblock %}

{% block extra_css %}
<style>
    .card-hover {
        transition: all 0.3s ease;
    }
    .card-hover:hover {
        transform: translateY(-4px);
        box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    }
    .table-row-hover:hover {
        background-color: #f9fafb;
        transition: background-color 0.2s ease;
    }
</style>
{% endblock %}

{% block content %}
<div class="bg-light min-vh-100">
    <!-- 页面标题 -->
    <div class="bg-white shadow-sm">
        <div class="container py-3">
            <h2 class="h3 mb-1">资产列表</h2>
            <div class="small text-muted">资产管理 <i class="fas fa-chevron-right mx-1"></i> 资产列表</div>
        </div>
    </div>

    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
        <!-- 筛选和视图切换 -->
        <div class="bg-white rounded-xl shadow-sm p-6 mb-6">
            <form method="get" id="searchForm">
                <div class="flex flex-col lg:flex-row lg:items-center lg:justify-between space-y-4 lg:space-y-0">
                    <div class="flex items-center space-x-4">
                        <div class="flex items-center">
                            <span class="text-sm text-gray-600 mr-2">视图:</span>
                            <button type="button" id="tableViewBtn" class="p-2 rounded bg-primary text-white">
                                <i class="fas fa-table"></i>
                            </button>
                            <button type="button" id="cardViewBtn" class="p-2 rounded text-gray-600 hover:bg-gray-100 ml-1">
                                <i class="fas fa-th-large"></i>
                            </button>
                        </div>
                        <div class="relative">
                            <select name="status" class="pl-3 pr-10 py-2 rounded-lg border border-gray-300 focus:border-primary focus:ring-2 focus:ring-primary/20 focus:outline-none appearance-none">
                                <option value="">全部状态</option>
                                {% for status_key, status_value in status_choices %}
                                    <option value="{{ status_key }}" {% if status_filter == status_key %}selected{% endif %}>
                                        {{ status_value }}
                                    </option>
                                {% endfor %}
                            </select>
                            <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
                                <i class="fas fa-chevron-down text-xs"></i>
                            </div>
                        </div>
                        <div class="relative">
                            <select name="category" class="pl-3 pr-10 py-2 rounded-lg border border-gray-300 focus:border-primary focus:ring-2 focus:ring-primary/20 focus:outline-none appearance-none">
                                <option value="">全部类型</option>
                                {% for category in categories %}
                                    <option value="{{ category.id }}" {% if category_filter|add:"0" == category.id %}selected{% endif %}>
                                        {{ category.name }}
                                    </option>
                                {% endfor %}
                            </select>
                            <div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700">
                                <i class="fas fa-chevron-down text-xs"></i>
                            </div>
                        </div>
                    </div>
                    <div class="flex items-center space-x-4">
                        <div class="relative w-full md:w-64">
                            <span class="absolute inset-y-0 left-0 flex items-center pl-3">
                                <i class="fas fa-search text-gray-400"></i>
                            </span>
                            <input type="text" name="search" value="{{ search_query }}" class="w-full pl-10 pr-4 py-2 rounded-lg border border-gray-300 focus:border-primary focus:ring-2 focus:ring-primary/20 focus:outline-none" placeholder="搜索资产...">
                        </div>
                        <button type="submit" class="px-4 py-2 bg-primary text-white font-medium rounded-lg hover:shadow-lg transition-all duration-300">
                            <i class="fas fa-filter mr-2"></i>筛选
                        </button>
                        <a href="{% url 'assets:asset_create' %}" class="px-4 py-2 bg-success text-white font-medium rounded-lg hover:shadow-lg transition-all duration-300">
                            <i class="fas fa-plus mr-2"></i>新增资产
                        </a>
                    </div>
                </div>
            </form>
        </div>

        <!-- 表格视图 -->
        <div id="tableView" class="bg-white rounded-xl shadow-sm p-6 mb-6">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead>
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">资产编号</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">资产名称</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">类型</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">状态</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">使用人</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">部门</th>
                            <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">启用日期</th>
                            <th class="px-4 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        {% for asset in page_obj %}
                        <tr class="table-row-hover">
                            <td class="px-4 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                                <span class="badge bg-info text-dark">{{ asset.asset_number }}</span>
                            </td>
                            <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
                                <div class="flex items-center">
                                    {% if asset.image %}
                                        <img class="h-8 w-8 rounded-full object-cover mr-3" src="{{ asset.image.url }}" alt="{{ asset.name }}">
                                    {% else %}
                                        <div class="h-8 w-8 rounded-full bg-gray-200 flex items-center justify-center mr-3">
                                            <i class="fas fa-cube text-gray-400 text-xs"></i>
                                        </div>
                                    {% endif %}
                                    <span class="font-medium">{{ asset.name }}</span>
                                </div>
                            </td>
                            <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">
                                <span class="badge bg-secondary">{{ asset.category.name|default:"-" }}</span>
                            </td>
                            <td class="px-4 py-4 whitespace-nowrap">
                                {% if asset.status == 'in_use' %}
                                    <span class="badge bg-primary">在用</span>
                                {% elif asset.status == 'idle' %}
                                    <span class="badge bg-success">闲置</span>
                                {% elif asset.status == 'maintenance' %}
                                    <span class="badge bg-warning text-dark">维护中</span>
                                {% elif asset.status == 'scrapped' %}
                                    <span class="badge bg-secondary">已报废</span>
                                {% endif %}
                            </td>
                            <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">{% if asset.user %}{{ asset.user.username }}{% else %}-{% endif %}</td>
                            <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">{{ asset.department.name|default:"-" }}</td>
                            <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-500">{{ asset.purchase_date|date:"Y-m-d" }}</td>
                            <td class="px-4 py-4 whitespace-nowrap text-right text-sm font-medium">
                                <a href="{% url 'assets:asset_detail' asset.id %}" class="text-primary hover:text-primary/80 mr-3">查看</a>
                                <a href="{% url 'assets:asset_edit' asset.id %}" class="text-gray-600 hover:text-gray-900 mr-3">编辑</a>
                                {% if asset.qr_code %}
                                    <a href="{% url 'assets:asset_qr_code' asset.id %}" class="text-info hover:text-info/80 mr-3">二维码</a>
                                {% endif %}
                                <button onclick="confirmDelete('{{ asset.id }}', '{{ asset.name }}')" class="text-danger hover:text-danger/80">删除</button>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="px-4 py-8 text-center text-gray-500">
                                <i class="fas fa-inbox text-4xl mb-4 text-gray-300"></i>
                                <p>暂无资产数据</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>

        <!-- 卡片视图 (默认隐藏) -->
        <div id="cardView" class="d-none">
            {% for asset in page_obj %}
            <div class="bg-white rounded-xl shadow-sm overflow-hidden card-hover">
                <div class="h-48 bg-gray-100 relative">
                    {% if asset.image %}
                        <img src="{{ asset.image.url }}" alt="{{ asset.name }}" class="w-full h-full object-cover">
                    {% else %}
                        <div class="w-full h-full flex items-center justify-center bg-gradient-to-br from-gray-100 to-gray-200">
                            <i class="fas fa-cube text-gray-400 text-4xl"></i>
                        </div>
                    {% endif %}
                    <div class="absolute top-3 right-3">
                        {% if asset.status == 'in_use' %}
                            <span class="badge bg-primary">在用</span>
                        {% elif asset.status == 'idle' %}
                            <span class="badge bg-success">闲置</span>
                        {% elif asset.status == 'maintenance' %}
                            <span class="badge bg-warning text-dark">维护中</span>
                        {% elif asset.status == 'scrapped' %}
                            <span class="badge bg-secondary">已报废</span>
                        {% endif %}
                    </div>
                </div>
                <div class="p-4">
                    <h3 class="text-lg font-semibold text-gray-900 mb-1">{{ asset.name }}</h3>
                    <p class="text-sm text-gray-500 mb-3">
                        <span class="badge bg-info text-dark">{{ asset.asset_number }}</span>
                    </p>
                    <div class="grid grid-cols-2 gap-2 mb-4">
                        <div class="bg-gray-50 rounded-lg p-2">
                            <p class="text-xs text-gray-500 mb-1">类型</p>
                            <p class="text-sm font-medium text-gray-900">{{ asset.category.name|default:"-" }}</p>
                        </div>
                        <div class="bg-gray-50 rounded-lg p-2">
                            <p class="text-xs text-gray-500 mb-1">使用人</p>
                            <p class="text-sm font-medium text-gray-900">{% if asset.user %}{{ asset.user.username }}{% else %}-{% endif %}</p>
                        </div>
                        <div class="bg-gray-50 rounded-lg p-2">
                            <p class="text-xs text-gray-500 mb-1">部门</p>
                            <p class="text-sm font-medium text-gray-900">{{ asset.department.name|default:"-" }}</p>
                        </div>
                        <div class="bg-gray-50 rounded-lg p-2">
                            <p class="text-xs text-gray-500 mb-1">启用日期</p>
                            <p class="text-sm font-medium text-gray-900">{{ asset.purchase_date|date:"Y-m-d" }}</p>
                        </div>
                    </div>
                    <div class="flex justify-between">
                        <a href="{% url 'assets:asset_detail' asset.id %}" class="px-3 py-1.5 text-primary border border-primary rounded-lg text-sm hover:bg-primary/5 transition-all duration-300">查看详情</a>
                        <div class="flex space-x-1">
                            <a href="{% url 'assets:asset_edit' asset.id %}" class="p-1.5 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-lg transition-colors">
                                <i class="fas fa-pencil"></i>
                            </a>
                            {% if asset.qr_code %}
                                <a href="{% url 'assets:asset_qr_code' asset.id %}" class="p-1.5 text-info hover:text-info/80 hover:bg-info/5 rounded-lg transition-colors">
                                    <i class="fas fa-qrcode"></i>
                                </a>
                            {% endif %}
                            <button onclick="confirmDelete('{{ asset.id }}', '{{ asset.name }}')" class="p-1.5 text-danger hover:text-danger/80 hover:bg-danger/5 rounded-lg transition-colors">
                                <i class="fas fa-trash"></i>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            {% empty %}
            <div class="col-span-full text-center py-12">
                <i class="fas fa-inbox text-6xl text-gray-300 mb-4"></i>
                <p class="text-gray-500 text-lg">暂无资产数据</p>
            </div>
            {% endfor %}
        </div>

        <!-- 分页 -->
        {% if page_obj.has_other_pages %}
        <nav aria-label="页面导航" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}"><i class="fas fa-chevron-left"></i></a>
                    </li>
                {% endif %}
                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                        <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                        <li class="page-item"><a class="page-link" href="?page={{ num }}{% if search_query %}&search={{ search_query }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}">{{ num }}</a></li>
                    {% endif %}
                {% endfor %}
                {% if page_obj.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}{% if category_filter %}&category={{ category_filter }}{% endif %}{% if status_filter %}&status={{ status_filter }}{% endif %}"><i class="fas fa-chevron-right"></i></a>
                    </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<!-- 确认删除模态框 -->
<div id="deleteModal" class="d-none position-fixed top-0 bottom-0 start-0 end-0 d-flex align-items-center justify-content-center z-3">
    <div class="position-absolute top-0 bottom-0 start-0 end-0 bg-dark bg-opacity-50"></div>
    <div class="bg-white rounded shadow p-4 w-100" style="max-width: 520px; position: relative; z-index: 10;">
        <div class="text-center">
            <div class="d-inline-flex align-items-center justify-content-center rounded-circle bg-danger bg-opacity-10 text-danger mb-3" style="width:64px;height:64px;">
                <i class="fas fa-exclamation-triangle fs-4"></i>
            </div>
            <h3 class="h5 mb-2">确认删除</h3>
            <p class="text-muted mb-4">您确定要删除该资产吗？此操作无法撤销。</p>
            <div class="d-flex justify-content-center gap-3">
                <button id="cancelDelete" class="btn btn-light">
                    取消
                </button>
                <button id="confirmDelete" class="btn btn-danger">
                    确认删除
                </button>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 视图切换功能
function switchView(viewType) {
    const tableView = document.getElementById('tableView');
    const cardView = document.getElementById('cardView');
    const tableBtn = document.getElementById('tableViewBtn');
    const cardBtn = document.getElementById('cardViewBtn');
    
    if (viewType === 'table') {
        tableView.classList.remove('hidden');
        cardView.classList.add('hidden');
        tableBtn.classList.add('bg-primary', 'text-white');
        tableBtn.classList.remove('text-gray-600', 'hover:bg-gray-100');
        cardBtn.classList.remove('bg-primary', 'text-white');
        cardBtn.classList.add('text-gray-600', 'hover:bg-gray-100');
        localStorage.setItem('assetViewMode', 'table');
    } else {
        tableView.classList.add('hidden');
        cardView.classList.remove('hidden');
        cardBtn.classList.add('bg-primary', 'text-white');
        cardBtn.classList.remove('text-gray-600', 'hover:bg-gray-100');
        tableBtn.classList.remove('bg-primary', 'text-white');
        tableBtn.classList.add('text-gray-600', 'hover:bg-gray-100');
        localStorage.setItem('assetViewMode', 'card');
    }
}

// 视图切换按钮事件
document.getElementById('tableViewBtn').addEventListener('click', function() {
    switchView('table');
});

document.getElementById('cardViewBtn').addEventListener('click', function() {
    switchView('card');
});

// 删除确认功能
let deleteAssetId = null;

function confirmDelete(assetId, assetName) {
    deleteAssetId = assetId;
    document.getElementById('deleteModal').classList.remove('hidden');
}

document.getElementById('cancelDelete').addEventListener('click', function() {
    document.getElementById('deleteModal').classList.add('hidden');
    deleteAssetId = null;
});

document.getElementById('confirmDelete').addEventListener('click', function() {
    if (deleteAssetId) {
        // 这里可以添加实际的删除逻辑
        alert('资产删除功能需要后端支持');
        document.getElementById('deleteModal').classList.add('hidden');
        deleteAssetId = null;
    }
});

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 恢复用户的视图偏好
    const savedViewMode = localStorage.getItem('assetViewMode');
    if (savedViewMode) {
        switchView(savedViewMode);
    }
    
    // 筛选器变化时自动提交表单
    document.querySelector('select[name="status"]').addEventListener('change', function() {
        document.getElementById('searchForm').submit();
    });
    
    document.querySelector('select[name="category"]').addEventListener('change', function() {
        document.getElementById('searchForm').submit();
    });
    
    // 搜索输入框回车提交
    document.querySelector('input[name="search"]').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            e.preventDefault();
            document.getElementById('searchForm').submit();
        }
    });
});

// 键盘快捷键支持
document.addEventListener('keydown', function(e) {
    // Ctrl + N 新增资产
    if (e.ctrlKey && e.key === 'n') {
        e.preventDefault();
        window.location.href = "{% url 'assets:asset_create' %}";
    }
    
    // Ctrl + F 聚焦搜索框
    if (e.ctrlKey && e.key === 'f') {
        e.preventDefault();
        document.querySelector('input[name="search"]').focus();
    }
    
    // Ctrl + 1 切换到表格视图
    if (e.ctrlKey && e.key === '1') {
        e.preventDefault();
        switchView('table');
    }
    
    // Ctrl + 2 切换到卡片视图
    if (e.ctrlKey && e.key === '2') {
        e.preventDefault();
        switchView('card');
    }
});
</script>
{% endblock %}
